// xc_lshop/shop/order/com/consignment.js
var app =getApp();
var actbill = require('../../model/order_bill')

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    themcolor:Object,
    rebateconfig:Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    consignid:"0",
    show:false,
    consignmentprice:"",
    op:'add'
  },

  /**
   * 组件的方法列表
   */
  methods: {
    orderop(e){
      // console.log(this.parents)
      // return 
      console.log(e)
      var data={};
      var orderid=this.data.orderinfo['id'];
      data['orderid']=orderid
      data['op']=e.currentTarget.dataset.op;
      actbill.consignmentop(data).then(res => {

        if (res["status"] == 1) {
          var title="修改成功";
          if(data['op']=='cancel'){
            title="取消寄售成功"
          }
          wx.showToast({
            title: title
          })
          this.close()
          this.triggerEvent('consignop')   
        } else {
          wx.showToast({
            title: "操作失败"
          })
        }

      })
      
    },
    submit(){
      var that=this;
      // var result=this.valide(this.data.consignmentprice)
      // console.log(result);
      // return
      var orderinfo=this.data.orderinfo;
      console.log("orderinfo")
      console.log(orderinfo)
      var data={};
      data['name']=orderinfo.orderinfo[0]["name"]
      data['orderid']=orderinfo['id'];
      data['ordernum']=orderinfo['ordernum'];
      if(this.data.consignid){
        data['id']=this.data.consignid
      }
      // if(orderinfo.consignmentprice){
      //   data['id']=orderinfo['id']
      // }
      if(this.data.consignmentprice){
        data['consignmentprice']=this.data.consignmentprice;
      }else{
        wx.showToast({
          icon:'none',
          title: "请填写寄售价格"
        })
        return;
      }
      actbill.consignmentbill(data).then(res => {
        if (res["status"] == 1) {
          wx.showToast({
            title: "寄售成功"
          })
          this.close()
          this.triggerEvent('consignop')   

        } else {
          wx.showToast({
            title: "操作失败"
          })
        }

      })
    },
    close(){
      this.setData({
        orderinfo:{},
        show:false,
        consignmentprice:""
      })
    },
    showmodel(item){
      console.log("item333")
      console.log(item)
      var update={};
      if(item.totalprice){ //订单列表寄售
        update['orderinfo']=item
        if(item.consignment && item.consignment.status==-1){
          update['op']='edit' 
          update['consignmentprice']= (item.consignment.consignmentprice / 100).toFixed(2)  
          update['consignid']= item.consignment.id  

        }
      }else{ //寄售列表
        update['orderinfo']=item.orderinfo
        if(item.status==-1){
          update['op']='edit' 
        }
        update['consignmentprice']= (item.consignmentprice / 100).toFixed(2)  
        update['consignid']= item.id  
      }

      // if(item.orderinfo){
      //   update['orderinfo']=item.orderinfo
      //   if(item.status==-1){
      //     update['op']='edit' 
      //   }
      //   update['consignmentprice']= (item.consignmentprice / 100).toFixed(2)  
      //   update['consignid']= item.id  
        
      // }else{
      //   update['orderinfo']=item
      //   if(item.consignment && item.consignment.status==-1){
      //     update['op']='edit' 
      //     update['consignmentprice']= (item.consignment.consignmentprice / 100).toFixed(2)  
      //   }
      //   update['consignid']= item.consignment.id  
      // }
      update['show']=true
     
      this.setData(update)
    },
    //验证金额
    valide: function(event){
      console.log(event)
      var value = event.detail;
      if (value.split('.')[0].length > 7) {
        if (value.split('.')[1]) {
          value = value.split('.')[0].substr(0, value.split('.')[0].length - 1) + '.' + value.split('.')[1];
        } else {
          value = value.split('.')[0].substr(0, value.split('.')[0].length - 1);
        }
      }
      value = value.replace(/[^\d\.]/g, ""); //清除"数字"和"."以外的字符
      value = value.replace(/^\./g, ""); //验证第一个字符是数字
      value = value.replace(/\.{2,}/g, "."); //只保留第一个, 清除多余的
      value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
      value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
      
      this.setData({
        'consignmentprice': value,
      })
    },
  }
})
